<?php defined('SYSPATH') OR die('No direct script access.');

class Controller_Manager_Media extends Controller {

	public function action_file()
        {	
            $this->load('assets');
	}
        
        private function load($path)
        {
            $file = $this->request->param('file');

            // Find the file extension
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            // Remove the extension from the filename
            $file = substr($file, 0, -(strlen($ext) + 1));

            if ($file = Kohana::find_file($path, $file, $ext))
            {
                
                // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
                $this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);

                // Send the file content as the response
                $this->response->body(file_get_contents($file));

                // Set the proper headers to allow caching
                $this->response->headers('content-type',  File::mime_by_ext($ext));
                $this->response->headers('last-modified', date('r', filemtime($file)));
            }
            else
            {
                $this->response->status(404);
            }            
        }

}
?>